angular.module('${menuBarCode}', ['ionic','oc.lazyLoad','util.service'])
	.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      cordova.plugins.Keyboard.disableScroll(true);

    }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleDefault();
    }
  });
});

angular.module('util.service',['ionic','ionic.closePopup'])
.factory('AppKit',['$http','$ionicModal','$ionicPopup','$timeout','$ionicPopover','IonicClosePopupService',function($http,$ionicModal,$ionicPopup,$timeout,$ionicPopover,IonicClosePopupService){
	var restUrlPrefix = "http://zhangyu-pc:8080";
	var service = {
		getConfig:function(key){
		   var consts = {};
		   consts.AppEntry = restUrlPrefix + "/portal/request/${menuBarCode}/index.ptml";
		   consts.MenuBarId = '${menuBarCode}';
		   return consts[key];
		},			
		getJsonApi:function(url,params){
			var fullURL = restUrlPrefix + url;
			var realParam = {};
			if (params){
				realParam = params;
			}
			var promise = $http.get(fullURL,realParam)
			/*
            promise.success(function(data, status, headers, config) {
               return data;
            }).
            error(function(data, status, headers, config) {
            	return {"error":status};
           });
           */
		   return promise;
		},
		postJsonApi:function(url,params){
			var fullURL = restUrlPrefix + url;
			var realParam = {};
			if (params){
				realParam = params;
			}
			var promise = $http.post(fullURL,realParam);
			/*
            promise.success(function(data, status, headers, config) {
                return data;
             }).
             error(function(data, status, headers, config) {
                return {"error":status};
            });
            */
			return promise;
		},
		successPopup:function($scope,popupTime){
			var popup = $ionicPopup.show({
		    title: '操作成功',
		    scope: $scope,
		    buttons: [{
	                 text: '关闭',
	                 type: 'button-positive',
	                 onTap: function(e) {
	                	 popup.close();           
	                 }
	               }
		    	]			     
			});
			
			if (popupTime){
				$timeout(function() {
					   popup.close();
				    }, 1000);				
			}else{
				$timeout(function() {
					   popup.close();
				    }, 3000);				
			}
		},
		errorPopup:function($scope){
			var popup = $ionicPopup.show({
		    title: '操作失败',
		    scope: $scope,
		    buttons: [{
	                 text: '关闭',
	                 type: 'button-assertive',
	                 onTap: function(e) {
	                	 popup.close();           
	                 }
	               }
		    	]			     
			});
		   $timeout(function() {
			   popup.close();
		   }, 3000);
		},		
		createModal:function(menuCode,$scope){
			var url = "/portal/request/${menuBarCode}/"+menuCode+".ptml";
			$ionicModal.fromTemplateUrl(url, {
				scope: $scope,
				animation: 'slide-in-left'
			}).then(function(modal) {
				$scope.modal = modal;
				$scope.modal.show();
				
				$scope.closeModal = function() {
				    $scope.modal.hide();
				    $scope.modal.remove();
				};
			})
		},	
		createPopup:function(menuCode,$scope){
			var currentPopup = $ionicPopup.show({
			    scope: $scope,
			    cssClass :"custom-popup",
			    templateUrl:"/portal/request/${menuBarCode}/"+menuCode+".ptml"	     
			});
			$scope.currentPopup = currentPopup;
			IonicClosePopupService.register(currentPopup);
		},
		closePopup:function($scope){
			$scope.currentPopup.close();
		},
		createPopover:function(menuCode,$scope,$event){
			if ($scope.popovers && $scope.popovers[menuCode]){
				$scope.popovers[menuCode].show($event);
			}else{
				var url = "/portal/request/${menuBarCode}/"+menuCode+".ptml";
				$scope.popovers = {};
				
				$ionicPopover.fromTemplateUrl(url, {
					scope: $scope
				}).then(function(popover){
					$scope.popovers[menuCode] = popover;
					$scope.popovers[menuCode].show($event);
					
					$scope.closePopover = function(menuCode) {
					    $scope.popovers[menuCode].hide();
					};		
					
					$scope.$on('$destroy', function() {
						for(var x in $scope.popovers){
							$scope.popovers[x].remove();
						}
					});
					
					$scope.$on('popover.removed', function() {
						
					});
					
					$scope.$on('popover.hidden', function() {
						
					});
				});
			}
		},
		addSwipeSupport:function($scope){
			$scope.tabs = [];
			$scope.activeTab = ''; 
			
			$scope.initTabs = function(tabs){
				$scope.tabs = tabs;	
				$scope.activeTab = tabs[0];
			}
			
			$scope.setActiveTab = function (activeTab) {     
			　　$scope.activeTab = activeTab; 
			};	
			
			$scope.onSwipeLeft = function(){
				for(var i=0;i< $scope.tabs.length;i++){
					var tempTab = $scope.tabs[i];
					if (tempTab == $scope.activeTab){
						if (i == ($scope.tabs.length-1))continue;
						
						$scope.activeTab = $scope.tabs[i+1];
						break;
					}
				}
			};

			$scope.onSwipeRight = function(){
				for(var i=0;i< $scope.tabs.length;i++){
					var tempTab = $scope.tabs[i];
					if (tempTab == $scope.activeTab){
						if (i == 0)continue;
						
						$scope.activeTab = $scope.tabs[i-1];
						break;
					}
				}
			};			
		}
	};
	return service;
}]);
;